package com.tgpms.web.system.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PutMapping;
import javax.servlet.http.HttpServletRequest;
import javax.annotation.Resource;
import com.tgpms.auth.security.TokenService;
import java.io.Serializable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.tgpms.common.Result;
import com.tgpms.web.system.entity.HbUsersMenu;
import com.tgpms.web.system.service.IHbUsersMenuService;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 环保用户菜单关联表 控制层
 * </p>
 *
 * @author newway
 * @since 2020-09-14
 */
@Api(value = "HbUsersMenuController", tags = "环保用户菜单相关接口")
@RestController
@RequestMapping("/ev/HbUsersMenu")
public class HbUsersMenuController {

	private final Logger log = LoggerFactory.getLogger(HbUsersMenuController.class);
	
	@Resource
	private IHbUsersMenuService usersMenuService;
	@Resource
	private TokenService tokenService;
	
	/**
	 * @param username 用户名
	 * @return 
	 */
	@ApiOperation(value = "配置查询用户菜单权限树形接口",notes = "配置查询用户菜单权限树形接口",httpMethod = "POST")
	@PostMapping(value = "/getHbUsersMenuInfoForTree")
	public Result getHbUsersMenuInfoForTree(@RequestParam(required = true)String username) {
		Result result = new Result();
		return usersMenuService.getHbUsersMenuInfoForTree(username, result);
	}
	
	@ApiOperation(value = "查询用户菜单集合接口",notes = "查询用户菜单集合接口",httpMethod = "POST")
	@PostMapping(value = "/getHbUsersMenus")
	public Result getHbUsersMenus(@RequestParam(required = true)String username,
			@RequestParam(required = true,defaultValue = "default")String type) {
		Result result = new Result();
		return usersMenuService.getHbUsersMenus(username,type, result);
	}
	
	
	@ApiOperation(value = "新增环保用户菜单关联表接口",notes = "新增环保用户菜单关联表接口",httpMethod = "POST")
	@PostMapping(value = "/addHbUsersMenuInfo")
	public Result addHbUsersMenuInfo(@RequestBody HbUsersMenu en,HttpServletRequest request) {
		log.info("新增环保用户菜单关联表接口接收到的参数为: "+en.toString());
		Result result = new Result();
		try{
			String insertName = tokenService.getUserName(request);
			en.setInsertUser(insertName);
			result = usersMenuService.addHbUsersMenu(en,result);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg("增加异常");
			return result;
		}
		return result;
	}
	
	@ApiOperation(value = "修改环保用户菜单关联表接口",notes = "修改环保用户菜单关联表接口",httpMethod = "PUT")
	@PutMapping(value = "/updateHbUsersMenuInfo")
	public Result updateHbUsersMenuInfo(@RequestBody HbUsersMenu en,HttpServletRequest request) {
		log.info("修改环保用户菜单关联表接口接收到的参数为: "+en.toString());
		Result result = new Result();
		try{
			String insertName = tokenService.getUserName(request);
			en.setInsertUser(insertName);
			en.setModifyUser(insertName);
			result = usersMenuService.updateHbUsersMenu(en,result);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg("修改异常");
			return result;
		}
		return result;
	}
	
	@ApiOperation(value = "删除环保用户菜单关联表接口",notes = "删除环保用户菜单关联表接口",httpMethod = "DELETE")
	@DeleteMapping(value = "/deleteHbUsersMenuInfo")
	public Result deleteHbUsersMenuInfo(@RequestParam Serializable id,HttpServletRequest request) {
		log.info("删除环保用户菜单关联表接口接收到的参数为: {id: "+id+" }");
		Result result = new Result();
		try{
			result = usersMenuService.deleteHbUsersMenu(id,result);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg("删除异常");
			return result;
		}
		return result;
	}
}
